﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Model_SSResourceManager
{
    public class ConnectSettingsManager
    {
        private string ConnectSettingsFileName = "ConnectSettings.set";

        ConnectSettingsModel Create()
        {
            return new ConnectSettingsModel()           // default settings
            {
                Network = NetworkType.Shared_Memory,
                Server ="(local)",
                ip_port= 0,
                SqlServAu = false,
                UserName ="",
                Password="",
                DbName = "SSRMDB"
            };
        }

        public ConnectSettingsModel Get()     // get from file or default
        {
            ConnectSettingsModel _connSets = Create();

            if (File.Exists(ConnectSettingsFileName))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConnectSettingsModel));
                using (StreamReader fileToRead = new StreamReader(ConnectSettingsFileName))
                {
                    _connSets = (ConnectSettingsModel)xmlSerializer.Deserialize(fileToRead);
                    fileToRead.Close();
                }
            }
            return _connSets;
        }

        public void Save(ConnectSettingsModel _connSets)   // Save To File
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConnectSettingsModel));

            using (StreamWriter saveInFile = new StreamWriter(ConnectSettingsFileName, false, Encoding.UTF8))
            {
                xmlSerializer.Serialize(saveInFile, _connSets);
                saveInFile.Close();
            }
        }
    }
}
